package com.Tian.controller;

import com.Tian.dao.UserInfoMapper;
import com.Tian.model.UserInfo;
import com.Tian.model.UserInfoParams;
import com.Tian.service.ExcelService;
import com.Tian.service.RoleInfoService;
import com.Tian.service.UserService;
import com.Tian.utile.SmsSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/test")
public class testController {

    @Autowired
    UserService userService;
    @Autowired
    SmsSend smsSend;

    //注入上传文件的绝对物理目录路径
    @Value("${file.address}")
    private String fileAddres;

    //注入访问上传文件的静态资源路径
    @Value("${file.staticPath}")
    private String fileStaicPath;

    @Autowired
    RoleInfoService roleInfoService;

    @RequestMapping("/login")
    public String login(){
        return "/layui/login";
    }
//将数据库里的值添加到表单上
    @RequestMapping("/list")
    public String list(Model model,UserInfo userInfo){
        model.addAttribute("list",userService.select(userInfo));
        return "jsp/list";
    }
    //发送短信
    @RequestMapping("/sendSms")
    @ResponseBody
    public HashMap<String,Object> sendSms(String phone, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        Random rd = new Random();
        int code = rd.nextInt(10000);
        //发送短信
        String info = smsSend.send(phone,code+"");
        //判断是否成功
        if(info.equals("OK")){
            //存入session中，通过session存储数据进行对比
            request.getSession().setAttribute("smsCode",code);
            map.put("info","短信发送成功");
        }else{
            map.put("info","短信发送失败");
        }

        return map;
    }

    //手机短信登录
    @RequestMapping("/smsLogin")
    @ResponseBody
    public HashMap<String,Object> smsLogin(String tel,String code, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        String sessionCode = request.getSession().getAttribute("smsCode")+"";
        if(code.equals(sessionCode)){
            //根据电话去查询用户的名，并存入到session中
            request.getSession().setAttribute("user",tel);
            map.put("info","登录成功");
        }else{
            map.put("info","登录失败");
        }
        return map;
    }
//访问到主页面
    @RequestMapping("/index")
    public String p(){
        return "layui/index";
    }

    //访问新增页面
    @RequestMapping("/add")
    public String add(Model model){
        model.addAttribute("roleList",roleInfoService.selectRoleService());
        return "/jsp/add";
    }

    //图片上传
    @ResponseBody
    @RequestMapping("/upload")
    public Map upload(MultipartFile file){
        //记录图片后缀
        String prefix="";
        //记录当天日期
        String dateStr="";
        try{
            if(file!=null){
                //获取上传图片的后缀 .jpg
                String originalName = file.getOriginalFilename();
                prefix=originalName.substring(originalName.lastIndexOf(".")+1);

                //生成随机一个uuid字符串作为上传图片的名称，保持图片名称的唯一性
                String uuid = UUID.randomUUID()+"";
                //获取当天日期作为上传图片的上一级目录
                Date date = new Date();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = simpleDateFormat.format(date);
                //得到上传图片的全路径
                String filepath = fileAddres+"\\" + dateStr+"\\"+uuid+"." + prefix;
                //打印查看上传路径
                System.out.println(filepath);

                File files=new File(filepath);
                //判读目录是否存在，不存在就创建
                if(!files.isDirectory()){
                    files.mkdirs();
                }
                //上传图片
                file.transferTo(files);
                //记录图片在项目中的路径，状态码，并传到前端
                Map<String,Object> map2=new HashMap<>();
                map2.put("src",fileStaicPath+"/"+ dateStr+"/"+uuid+"." + prefix);
                Map<String,Object> map=new HashMap<>();
                map.put("code",0);
                map.put("msg","");
                map.put("data",map2);

                return map;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        //失败
        Map<String,Object> map=new HashMap<>();
        map.put("code",1);
        map.put("msg","");
        return map;
    }

    //实现新增调用操作
    @RequestMapping("/addRole")
    @ResponseBody
    public HashMap<String,Object> addRole(UserInfoParams userInfoParams){

        return userService.addRole(userInfoParams);
    }


    //修改
    @ResponseBody
    @RequestMapping("/Update")
    public String update(Model model, Integer uId){
        model.addAttribute("roleList",roleInfoService.selectRoleService());
        model.addAttribute("userInfo",userService.selectUserId(uId));
        return "/jsp/update";
    }
    @Autowired
    ExcelService excelService;
    //excel导出
    @RequestMapping("/excel")
    public void excelWrite(HttpServletResponse response){
        excelService.excel(response);
    }
}
